package com.example.baseproject.easyhttptest;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.example.baseproject.main.api.MatchApi;
import com.example.baseproject.main.api.MatchListApi;
import com.example.baseproject.main.api.PedalApi;
import com.example.baseproject.main.bean.TestOneBean;
import com.example.baseproject.main.bean.TestTwoBean;
import com.example.baseproject.main.bean.ThreeBean;
import com.hjq.http.EasyHttp;
import com.hjq.http.config.IRequestApi;
import com.tjcd.project.library_base.base.bean.BaseHttpBean;
import com.tjcd.project.library_base.base.bean.BaseHttpPageBean;
import com.tjcd.project.library_base.base.bean.Resource;
import com.tjcd.project.library_base.base.easyhttpnet.EasyHttpCallbackProxy;
import com.tjcd.project.library_base.base.model.BaseViewModel;

import java.util.List;

import lombok.Getter;

/**
 * @Author jiangKunKun
 * @CreateDate 2024/9/27
 * @Description
 * @SpittingGrooves
 */
@Getter
public class TestViewModel extends BaseViewModel {
    public TestViewModel(@NonNull Application application) {
        super(application);
    }

    private MutableLiveData<Resource<TestOneBean>> testOneBeanLiveData = new MutableLiveData<>();

    /**
     * 测试
     */
    public void testOneBean() {
        /**
         * .get(ApplicationLifecycle.getInstance())
         *                 .tag("关闭页面,继续请求")
         * EasyHttp.cancel("关闭页面,继续请求");
         */
        /**
         *  Json 数组作为参数进行上传
         * List<Xxx> parameter = new ArrayList<>();
         * list.add(xxx);
         * list.add(xxx);
         * String json = gson.toJson(parameter);
         * .body(new JsonBody(json))
         */
        LogUtils.e("testOneBean");
        EasyHttp.get(this)
                .api(new MatchApi())
                .request(new EasyHttpCallbackProxy<BaseHttpBean<TestOneBean>>() {
                    @Override
                    public void onHttpEnd(@NonNull IRequestApi api) {
                        super.onHttpEnd(api);
                        testOneBeanLiveData.setValue(Resource.finish());
                    }

                    @Override
                    public void onHttpStart(@NonNull IRequestApi api) {
                        super.onHttpStart(api);
                        testOneBeanLiveData.setValue(Resource.start());
                    }

                    @Override
                    public void onDataSuccess(BaseHttpBean<TestOneBean> data) {
                        testOneBeanLiveData.setValue(ObjectUtils.isNotEmpty(data.getData()) ?
                                Resource.dataSuccess(data.getData()) : Resource.dataFail(data.getData()));
                    }

                    @Override
                    public void onDataFail(int code, String message, BaseHttpBean<TestOneBean> data) {
                        testOneBeanLiveData.setValue(Resource.dataFail(Integer.valueOf(code), message));
                    }
                });

    }

    private MutableLiveData<Resource<BaseHttpPageBean<TestTwoBean>>> twoDataBean = new MutableLiveData<>();
    public void testTwoBean() {
        // matchData.postValue(Resource.loading());
        EasyHttp.get(this)
                .api(new MatchListApi().setPage("1").setLimit("10").setMatchId("1896100946651951106"))
                .request(new EasyHttpCallbackProxy<BaseHttpBean<BaseHttpPageBean<TestTwoBean>>>() {
                    @Override
                    public void onDataSuccess(BaseHttpBean<BaseHttpPageBean<TestTwoBean>> result) {
                        twoDataBean.setValue(Resource.dataSuccess(result.getData()));
                    }

                    @Override
                    public void onDataFail(int code, String msg, BaseHttpBean<BaseHttpPageBean<TestTwoBean>> result) {
                        twoDataBean.setValue(Resource.dataFail(code, msg, result.getData()));
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        twoDataBean.setValue(Resource.error(throwable.getMessage()));
                    }

                    @Override
                    public void onHttpEnd(@NonNull IRequestApi api) {
                        twoDataBean.setValue(Resource.finish());
                    }
                });
    }

    private MutableLiveData<Resource<List<ThreeBean>>> threeBean = new MutableLiveData<>();
    public void testThreeBean() {
        // matchData.postValue(Resource.loading());
        EasyHttp.get(this)
                .api(new PedalApi().setShedId("1895378794567438338"))
                .request(new EasyHttpCallbackProxy<BaseHttpBean<List<ThreeBean>>>() {
                    @Override
                    public void onHttpStart(@NonNull IRequestApi api) {
                        super.onHttpStart(api);
                        threeBean.setValue(Resource.start());
                    }

                    @Override
                    public void onDataSuccess(BaseHttpBean<List<ThreeBean>> data) {
                        threeBean.setValue(Resource.dataSuccess(ObjectUtils.isNotEmpty(data.getData()) ? data.getData() : null));
                    }

                    @Override
                    public void onDataFail(int code, String message, BaseHttpBean<List<ThreeBean>> data) {
                        threeBean.setValue(Resource.dataFail(code, message));
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        super.onHttpFail(throwable);
                        threeBean.setValue(Resource.error(throwable.getMessage()));
                    }

                    @Override
                    public void onHttpEnd(@NonNull IRequestApi api) {
                        super.onHttpEnd(api);
                        threeBean.setValue(Resource.finish());
                    }
                });
    }

}
